<template>
  <div class="wrap">
    <h1 class="title"><span class="gradient-text">创建 PageConfig</span></h1>

    <div class="editor-card">
      <!-- 背景图上传 -->
      <el-upload
        class="uploader"
        action="/api/api/upload"
        name="files"
        :show-file-list="false"
        :on-success="handleBgSuccess"
        :before-upload="beforeUpload"
      >
        <img v-if="form.backgroundUrl" :src="form.backgroundUrl" class="bg-preview" />
        <div v-else class="uploader-tips">
          <i class="el-icon-plus" /> 点击上传背景图
        </div>
      </el-upload>

      <el-input v-model="form.title" placeholder="标题" class="m-t" />
      <el-input v-model="form.subTitle" placeholder="副标题" class="m-t" />
      <el-input
        v-model="form.speakerContent"
        type="textarea"
        :rows="3"
        placeholder="语音文字"
        class="m-t"
      />

      <!-- 音色 -->
      <el-select
        v-model="form.voiceCode"
        placeholder="选择音色"
        style="width: 100%; margin-top: 10px"
      >
        <el-option
          v-for="v in voiceList"
          :key="v.code"
          :label="v.name"
          :value="v.code"
        />
      </el-select>

      <!-- 按钮组：完全独立，不影响其他页面 -->
      <div class="create-btn-group">
        <el-button
          type="success"
          :loading="generating"
          @click="handleGenerate"
        >
          一键生成副标题与语音文字
        </el-button>
        <el-button type="primary" @click="submit">创建</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { addConfig, getVoiceDropdown,generate } from '@/api/index'

export default {
  name: 'CreatePageConfig',
  data() {
    return {
      form: {
        backgroundUrl: '',
        title: '',
        subTitle: '',
        speakerContent: '',
        voiceCode: ''
      },
      voiceList: [],
      generating: false
    }
  },
  created() {
    this.loadVoice()
  },
  computed: {
    // 总开关：所有必填项都已填写
    canSubmit() {
      return (
        this.form.backgroundUrl.trim() !== '' &&
        this.form.title.trim() !== '' &&
        this.form.subTitle.trim() !== '' &&
        this.form.speakerContent.trim() !== '' &&
        this.form.voiceCode.trim() !== ''
      )
    }
  },
  methods: {
    loadVoice() {
      getVoiceDropdown().then(res => (this.voiceList = res.data))
    },
    beforeUpload(file) {
      const isImg = file.type.startsWith('image/')
      if (!isImg) this.$message.error('只能上传图片')
      return isImg
    },
    handleBgSuccess(res) {
      this.form.backgroundUrl = res.data[0]
    },
    async handleGenerate() {
      if (!this.form.title.trim()) {
        this.$message.warning('请填写标题')
        return
      }
      this.generating = true
      try {
        const { data } = await generate({ title: this.form.title })
        this.form.subTitle = data.subTitle
        this.form.speakerContent = data.speakerContent
        this.$message.success('生成成功')
      } catch {
        this.$message.error('生成失败，请重试')
      } finally {
        this.generating = false
      }
    },
    async submit() {
      if (!this.canSubmit) return
      const res = await addConfig(this.form)
      this.$message.success('创建成功')
      this.$emit('created', res.data)
    }
  }
}
</script>

<style scoped>
/* ---------- 通用样式 ---------- */
.m-t {
  margin-top: 16px;
}
.full {
  width: 100%;
}
.editor-card {
  max-width: 600px;
  margin: 0 auto;
  background: #fff;
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
}
.uploader {
  text-align: center;
}
.uploader-tips {
  width: 100%;
  height: 160px;
  line-height: 160px;
  border: 2px dashed #dcdfe6;
  border-radius: 8px;
  cursor: pointer;
  font-size: 28px;
  color: #8c939d;
}
.bg-preview {
  width: 100%;
  height: 160px;
  object-fit: cover;
  border-radius: 8px;
}

/* ---------- 按钮组：完全隔离 ---------- */
.create-btn-group {
  display: flex;
  gap: 12px;
  margin-top: 16px;
}
.create-btn-group .el-button {
  flex: 1;
  height: 40px;
}
</style>